package timberman.core;

import timberman.screens.SplashScreen;
import timberman.screens.TestScreen;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.BitmapFontLoader;
import com.badlogic.gdx.assets.loaders.MusicLoader;
import com.badlogic.gdx.assets.loaders.SkinLoader;
import com.badlogic.gdx.assets.loaders.TextureAtlasLoader;
import com.badlogic.gdx.assets.loaders.TextureLoader;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.ResolutionFileResolver.Resolution;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.XmlReader;
import com.badlogic.gdx.utils.XmlReader.Element;

public class MyGame extends Game {
	public static boolean soundOn = true, musicOn = true;
	public AssetManager manager;
	// public SoundManager sound;
	public float assumeWidth;
	public float assumeHeight;

	@Override
	public void create() {
		manager = new AssetManager();
		// sound = new SoundManager();
		batch = new SpriteBatch();
		fixResolution();
		setScreen(new SplashScreen());
	}

	private void fixResolution() {
		dimensions = new ObjectMap<String, ObjectMap<String, Float>>();
		Resolution[] descriptors = {
		// new Resolution(240, 320, ".240320"),
		// new Resolution(240, 400, ".240400"),
		// new Resolution(240, 432, ".240432"),
		// new Resolution(320, 480, ".320480"),
		// new Resolution(480, 640, ".480640"),
		// new Resolution(480, 800, ".480800"),
		// new Resolution(480, 854, ".480854"),
		// new Resolution(600, 1024, ".6001024"),
		new Resolution(640, 960, "640960"),
		// new Resolution(768, 1024, "7681024"),
		// new Resolution(640, 1136, "6401136"),
		// new Resolution(768, 1280, ".7681280"),
		};
		Resolution resolution = ResolutionFileResolver.choose(descriptors);
		resolver = new ResolutionFileResolver(new InternalFileHandleResolver(),
				resolution);
		manager.setLoader(Texture.class, new TextureLoader(resolver));
		manager.setLoader(TextureAtlas.class, new TextureAtlasLoader(resolver));
		manager.setLoader(BitmapFont.class, new BitmapFontLoader(resolver));
		manager.setLoader(Skin.class, new SkinLoader(resolver));
		manager.setLoader(Music.class, new MusicLoader(resolver));
		Debug.log("resolution: " + resolution.suffix);
		assumeWidth = resolution.portraitWidth;
		assumeHeight = resolution.portraitHeight;
	}

	ResolutionFileResolver resolver;

	public FileHandle resolve(String fileName) {
		return resolver.resolve(fileName);
	}

	ObjectMap<String, ObjectMap<String, Float>> dimensions;
	public SpriteBatch batch;

	public float getDimen(String fileName, String name) {
		if (!dimensions.containsKey(fileName)) {
			FileHandle f = resolve("data/dimensions/" + fileName + ".xml");
			if (f.exists()) {
				try {
					XmlReader reader = new XmlReader();
					Element root = reader.parse(f);
					Array<Element> dimens = root.getChildrenByName("dimen");
					ObjectMap<String, Float> dims = new ObjectMap<String, Float>();
					for (Element dimen : dimens) {
						String dname = dimen.get("name");
						float dvalue = dimen.getFloat("value");
						dims.put(dname, dvalue);
					}
					dimensions.put(fileName, dims);
				} catch (Exception e) {
					throw new RuntimeException(
							"Coudn't parse dimmension file definition: "
									+ fileName + ".xml");
				}
			} else {
				throw new RuntimeException(
						"Coudn't find dimmension file definition: " + fileName
								+ ".xml");
			}
		}
		ObjectMap<String, Float> values = dimensions.get(fileName);
		if (values.containsKey(name)) {
			return values.get(name);
		} else {
			throw new RuntimeException("Coudn't find the dimen: " + name
					+ " in " + fileName + ".xml");
		}
	}

	private static MyGame instance = null;

	public static MyGame getInstance() {
		if (instance == null) {
			instance = new MyGame();
		}
		return instance;
	}

	public static void setInstance(MyGame instance) {
		MyGame.instance = instance;
	}

	public BitmapFont vniMurray;
	public Skin skin;

	@Override
	public void render() {
		super.render();
		if (vniMurray != null) {
			batch.begin();
			vniMurray.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(),
					10, 100);
			batch.end();
		}
	}

	public void initSkin() {
//		skin = manager.get("data/graphics/common/uiskin.json", Skin.class);
	}
}
